package com.esm.sm800.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.esm.sm800.R;
import com.esm.sm800.database.dbmodel.Hiskeyrecord;
import com.esm.sm800.database.dbmodel.Member;
import com.esm.sm800.database.manager.HiskeyrecordCenter;
import com.esm.sm800.database.manager.MemberCenter;
import com.esm.sm800.database.manager.SystemCenter;
import com.esm.sm800.util.MyFunc;
import com.esm.sm800.util.NFCComm;
import com.esm.sm800.util.ZazManager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android_serialport_api.ComBean;
import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.BottomMenuWindow;
import zuo.biao.library.util.EditTextUtil;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private final String TAG = "LoginActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.login_activity);

        et_name = (EditText) findViewById(R.id.et_name);
        et_pws = (EditText) findViewById(R.id.et_pws);
        tv_title = (TextView) findViewById(R.id.tv_title);
        m_txtStatus = (TextView) findViewById(R.id.txtStatus);
        m_FpImageViewer = (ImageView) findViewById(R.id.imvImgViewer);
        findViewById(R.id.bt_hisrecord).setOnClickListener(this);
        findViewById(R.id.tv_ok).setOnClickListener(this);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        findViewById(R.id.et_name).setOnClickListener(this);
        findViewById(R.id.et_pws).setOnClickListener(this);


        //初始化历史记录
        if (!initHisrecode()) {
            HIS_RECORDS = new String[1];
            HIS_RECORDS[0] = "admin";
        }
        String[] TYPES = getResources().getStringArray(R.array.login_types);
        if (SystemCenter.getInstance().logintype >= 0 && SystemCenter.getInstance().logintype < TYPES.length) {
            tv_title.setText(TYPES[SystemCenter.getInstance().logintype]);
        }

        et_name.setFocusable(false);
        et_pws.setFocusable(false);

        ZazManager.getInstance().m_txtStatus = m_txtStatus;
        ZazManager.getInstance().m_FpImageViewer = m_FpImageViewer;
        ZazManager.getInstance().mHandler = mHandler;
        ZazManager.getInstance().mNeedbreak = false;
        ZazManager.getInstance().identify();

        NFCComm.getInstance().mHandler = mHandler;
    }

    private void testZh() {
        Member member = MemberCenter.getInstance().getMember("admin");
        String s = member.getPassword();
        int type = member.getType();
    }

    private EditText et_name;
    private EditText et_pws;

    TextView tv_title;
    TextView m_txtStatus;
    ImageView m_FpImageViewer;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bt_hisrecord:
                onHisrecode();
                break;
            case R.id.tv_ok:
                onLogin();
                break;
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.et_name:
                EditTextUtil.showKeyboard(context, et_name,true);
                break;
            case R.id.et_pws:
                EditTextUtil.showKeyboard(context, et_pws, true);
                break;
        }
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == ZazManager.FINISH_SB) {
                finishSB();
            } else if (msg.what == NFCComm.RECEIVED_MSG) {
                showMsg(NFCComm.getInstance().mComRecData);
            }
        }
    };

    private void showMsg(ComBean comRecData) {
        if (m_txtStatus == null || comRecData == null) {
            return;
        }
        String strHex = MyFunc.ByteArrToHex(comRecData.bRec);
        doNFC(strHex);
    }

    private void finishSB() {
        long id = ZazManager.getInstance().zhiwenID;
        if (id < 1) {
            return;
        }

        Member member = MemberCenter.getInstance().getMemberbyid(id);
        if (member == null) {
            showShortToast(getString(R.string.zwzhbcz));
            return;
        }

        if (SystemCenter.getInstance().logintype == 0) {
            if (member.getType() > 1) {
                showShortToast(getString(R.string.qsyglydzhdl));
                return;
            }
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 1) {
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 2) {
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 3) {
            if (member.getType() > 1) {
                showShortToast(getString(R.string.qsyglydzhdl));
                return;
            }
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 4) {
            if (member.getType() > 1) {
                showShortToast(getString(R.string.qsyglydzhdl));
                return;
            }
            SystemCenter.getInstance().memberTgr = member;
        }

        showShortToast(getString(R.string.dlcg));

        ZazManager.getInstance().mNeedbreak = true;

        setResult(RESULT_OK);
        finish();
    }

    private void doNFC(String strNFC) {
        if (strNFC.length() < 2) {
            return;
        }

        Member member = MemberCenter.getInstance().getMemberbynfc(strNFC);
        if (member == null) {
            showShortToast("NFC无效！");
            return;
        }

        if (SystemCenter.getInstance().logintype == 0) {
            if (member.getType() > 1) {
                showShortToast(getString(R.string.qsyglydzhdl));
                return;
            }
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 1) {
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 2) {
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 3) {
            if (member.getType() > 1) {
                showShortToast(getString(R.string.qsyglydzhdl));
                return;
            }
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 4) {
            if (member.getType() > 1) {
                showShortToast(getString(R.string.qsyglydzhdl));
                return;
            }
            SystemCenter.getInstance().memberTgr = member;
        }

        showShortToast(getString(R.string.dlcg));

        ZazManager.getInstance().mNeedbreak = true;

        setResult(RESULT_OK);
        finish();
    }

    protected void onResume() {
        super.onResume();
        ZazManager.getInstance().m_txtStatus = m_txtStatus;
        ZazManager.getInstance().m_FpImageViewer = m_FpImageViewer;
        ZazManager.getInstance().mHandler = mHandler;
        NFCComm.getInstance().mHandler = mHandler;
    }

    @Override
    protected void onPause() {
        super.onPause();
        ZazManager.getInstance().m_txtStatus = null;
        ZazManager.getInstance().m_FpImageViewer = null;
        ZazManager.getInstance().mHandler = null;
        ZazManager.getInstance().mNeedbreak = true;
        NFCComm.getInstance().mHandler = null;
    }

    private void onLogin() {
        String username = et_name.getText().toString();
        String mima = et_pws.getText().toString();

        if (username.length() < 1) {
            showShortToast(getString(R.string.sjm_bz_zh));
            return;
        }

        if (mima.length() < 1) {
            showShortToast(getString(R.string.sjm_bz_mm));
            return;
        }

        Member member = MemberCenter.getInstance().getMember(username);
        if (member == null) {
            showShortToast(getString(R.string.wcyh));
            return;
        }
        if (!member.getPassword().equals(mima)) {
            showShortToast(getString(R.string.mimacuowu));
            return;
        }

        if (SystemCenter.getInstance().logintype == 0) {
            if (member.getType() > 1) {
                showShortToast(getString(R.string.qsyglydzhdl));
                return;
            }
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 1) {
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 2) {
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 3) {
            if (member.getType() > 1) {
                showShortToast(getString(R.string.qsyglydzhdl));
                return;
            }
            SystemCenter.getInstance().member = member;
        } else if (SystemCenter.getInstance().logintype == 4) {
            if (member.getType() > 1) {
                showShortToast(getString(R.string.qsyglydzhdl));
                return;
            }
            SystemCenter.getInstance().memberTgr = member;
        }

        //保存历史关键字
        Hiskeyrecord hiskeyrecord = new Hiskeyrecord();
        hiskeyrecord.setName(username);
        hiskeyrecord.setType(0);
        //获取当前日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date();
        String strDate = formatter.format(curDate);
        //
        hiskeyrecord.setUpdate_time(strDate);
        HiskeyrecordCenter.getInstance().saveHiskeyrecord(hiskeyrecord);
        showShortToast(getString(R.string.dlcg));

        setResult(RESULT_OK);
        finish();
    }

    String[] HIS_RECORDS = null;

    private boolean initHisrecode() {
        ArrayList<Hiskeyrecord> list = HiskeyrecordCenter.getInstance().getHiskeyrecordlist(0);
        if (list == null) {
            return false;
        }
        int size = list.size();
        if (size < 1) {
            return false;
        }
        HIS_RECORDS = new String[list.size()];
        for (int i = 0; i < size; i++) {
            HIS_RECORDS[i] = list.get(i).getName();
        }

        return true;
    }

    private void onHisrecode() {
        if (HIS_RECORDS == null) {
            showShortToast(getString(R.string.wlsjl));
            return;
        }
        toActivity(BottomMenuWindow.createIntent(getBaseContext(), HIS_RECORDS)
                .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.lsjl)), REQUEST_TO_BOTTOM_MENU_1, false);
    }

    private final int REQUEST_TO_BOTTOM_MENU_1 = 31;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int pos = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (pos >= 0 && pos < HIS_RECORDS.length) {
                        String name = HIS_RECORDS[pos];
                        et_name.setText(name);
                    }
                }
                break;
        }
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }
}

